<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button v-if="isAuth('kernel:message:form')" type="success" icon="Plus" @click="router.push({ name: 'kernel_message_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button v-if="isAuth('kernel:message:delete')" type="danger" icon="Delete" @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_top">
        <div class="admin_main_block_select">
          <div class="input">
            <el-input v-model="dataForm.title" :placeholder="i18n('common.please_input') + i18n('message.title')" clearable>
            </el-input>
          </div>
          <div class="input">
            <el-button icon="Search" @click="getDataList(true)">
              {{ i18n('common.search') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle">
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column prop="type" :label="i18n('message.type')" width="100">
            <template v-slot="scope">
              <el-tag v-if="1 == scope.row.type.value">
                {{ scope.row.type.text }}
              </el-tag>
              <el-tag type="warning" v-else-if="2 == scope.row.type.value">
                {{ scope.row.type.text }}
              </el-tag>
              <el-tag type="success" v-else-if="3 == scope.row.type.value">
                {{ scope.row.type.text }}
              </el-tag>
              <el-tag type="danger" v-else>
                {{ scope.row.type.text }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column prop="title" :label="i18n('message.title')">
          </el-table-column>

          <el-table-column header-align="center" align="center" :label="i18n('message.receiver')">
            <template v-slot="scope">
              <el-popover trigger="hover" placement="top">
                <span v-for="(value, index) in scope.row.user" :label="index" :key="index">
                  <b v-if="value">{{ value.nickname }} &nbsp;&nbsp;</b>
                </span>
                <template v-slot:reference>
                  <div class="name-wrapper">
                    <el-tag size="medium">{{ i18n('common.view') }}</el-tag>
                  </div>
                </template>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column prop="author" :label="i18n('message.author')" width="120">
          </el-table-column>

          <el-table-column prop="create_time" header-align="center" align="center" :label="i18n('common.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="120">
            <template v-slot="scope">
              <el-button v-if="isAuth('kernel:message:delete')" type="danger" icon="Delete" @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="message-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, isAuth } = useBase()

  const model = ref('message')
  const dataForm = reactive([
    'title',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  onMounted(() => {
    getDataList(model)
  })
</script>
